package com.breeze.stack;

/**
 * 链栈
 * 头插法，插入和删除在同一端
 */
public class LinkStack<T> {
    Node<T> top = null;
    private int size;

    public T pop() {
        if (top == null) {
            System.out.println("栈为空");
            return null;
        }
        T res = top.val;
        top = top.next;
        size--;
        return res;
    }

    public void push(T data) {
        top = new Node<>(data, top);
        size++;
    }

    public int size() {
        return size;
    }

    public void clear() {
        top = null;
        size = 0;
    }

    public boolean isEmpty() {
        return top == null;
    }

    static class Node<T> {
        T val;
        Node<T> next;

        public Node(T val, Node<T> next) {
            this.val = val;
            this.next = next;
        }
    }
}
